﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
using BestBuy.Common;
using BestBuy.DTO;
namespace BestBuy.PageUser
{
    public partial class ManageSellOffer : System.Web.UI.Page
    {
        private static List<DTOSellOffer> lisell;
        protected void Page_Load(object sender, EventArgs e)
        {
            checkTonTai();
        }
        private void checkTonTai()
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!IsPostBack)
            {
                loadInfoPresent();
                loadListProduct();
            }  
        }
        private void loadInfoPresent()
        {
            int idUser = int.Parse(Session["IDDN"].ToString());
            int idcongty = cManageSellOffer.getIdCompany(idUser);
            getCountProduct.Text = cManageSellOffer.getCountSellOffer(idUser).ToString();
            getNameCompany.Text = cManageSellOffer.getNameCompany(idcongty).ToString();
        }
        private void loadListProduct()
        {
            lisell = new List<DTOSellOffer>();
            List<BangTinBan> li = cManageSellOffer.getListSellOffer(int.Parse(Session["IDDN"].ToString()));
            foreach (var item in li)
            {
                DTOSellOffer dt = new DTOSellOffer();
                dt.IdSellOffer = item.IDBangTinBan;
                dt.Title = item.TieuDe;
                dt.Datepost = item.NgayDang;
                dt.Experies = item.ThoiHanDang;
                dt.Status = Convert.ToInt32(item.TinhTrang);
                lisell.Add(dt);
            }
            gvListProduct.DataSource = lisell;
            gvListProduct.DataBind();
        }
        protected void gvListProduct_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //kiem tra unverified
                int status = (int)DataBinder.Eval(e.Row.DataItem, "Status");
                int IDSanPham = (int)DataBinder.Eval(e.Row.DataItem, "IdSellOffer");                
                switch (status)
                {
                    case (int)BangTinBanStatus.Active:
                        HyperLink link = e.Row.FindControl("LinkEdit") as HyperLink;
                        if (link != null)
                        {
                            link.Visible = true;
                            link.NavigateUrl = "EditSellOffer.aspx?IdSellOffer=" + IDSanPham;
                        }
                        break;
                    case (int)BangTinBanStatus.Unverified:
                        Literal lbUnverified = e.Row.FindControl("Unverified") as Literal;
                        if (lbUnverified != null)
                        {
                            lbUnverified.Visible = true;
                        }
                        break;
                }
            }
        }

        protected void Btremove_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvListProduct.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)gvListProduct.Rows[i].FindControl("chkXoa");
                if (cb.Checked)
                {
                    foreach (var ite in lisell)
                    {
                        if (ite.IdSellOffer == Convert.ToInt32(((Label)gvListProduct.Rows[i].FindControl("lbIdSanPham")).Text))
                        {
                            ite.Check = true;
                        }
                    }
                }
            }
            foreach (var item in lisell)
            {
                if (item.Check)
                {
                    cManageSellOffer.deleteSellOffer(item.IdSellOffer);
                }
            }
            loadListProduct();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            gvListProduct.DataSource = null;
            gvListProduct.DataBind();
            if (Txtsearch.Text != "")
            {
                lisell = new List<DTOSellOffer>();
                List<BangTinBan> li = cManageSellOffer.searchTitle(int.Parse(Session["IDDN"].ToString()), Txtsearch.Text);
                foreach (var item in li)
                {
                    DTOSellOffer dt = new DTOSellOffer();
                    dt.IdSellOffer = item.IDBangTinBan;
                    dt.Title = item.TieuDe;                    
                    dt.Datepost = item.NgayDang;
                    dt.Experies = item.ThoiHanDang;
                    dt.Status = item.TinhTrang;
                    lisell.Add(dt);
                }
                gvListProduct.DataSource = lisell;
                gvListProduct.DataBind();
            }
            else
            {
                loadListProduct();
            }
        }

        protected void Repost_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvListProduct.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)gvListProduct.Rows[i].FindControl("chkXoa");
                if (cb.Checked)
                {
                    foreach (var ite in lisell)
                    {
                        if (ite.IdSellOffer == Convert.ToInt32(((Label)gvListProduct.Rows[i].FindControl("lbIdSanPham")).Text))
                        {
                            ite.Check = true;
                        }
                    }
                }
            }
            foreach (var item in lisell)
            {
                if (item.Check)
                {
                    cManageSellOffer.rePost(item.IdSellOffer);
                }
            }
            loadListProduct();
        }
    }
}